package com.swq.java.code.chapter18.code19;

import com.swq.java.code.utils.Values;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class BufferTester {
    public static void main(String args[]) throws IOException {
        final int BSIZE = 1024;
        //把test.txt文件中的数据拷贝到out.txt中
        FileChannel in = new FileInputStream(Values.testFileName).getChannel();
        FileChannel out = new FileOutputStream(Values.outFileName).getChannel();

        ByteBuffer buff = ByteBuffer.allocate(BSIZE);
        int i = 0;
        while (in.read(buff) != -1) {
            System.out.println(i++);
            buff.flip();
            out.write(buff);
            buff.clear();
        }
        in.close();
        out.close();
    }
}